﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using estoque.ViewModel;

namespace estoque
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ITaskbarList3 taskBar = null;
        IntPtr handle = (IntPtr)0;

        private TimeAccount timeAccount { get { return this.timeAccountUc.TimeAccount; } }

        public MainWindow()
        {
            this.InitializeComponent();
            this.BindKeys();
            RepositoryVM.Center.Sort();
            this.DataContext = RepositoryVM.Center;
            this.ScenarioTabControl.SelectedIndex = 0;
            this.Loaded += this.Window_Loaded;
            this.Closed += this.Window_Closed;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;

            taskBar = (ITaskbarList3)new TaskbarList();

            if (source != null && timeAccount != null)
            {
                this.handle = source.Handle;
                timeAccount.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainWindow_PropertyChanged);

                this.UpdateTaskbar();
            }
        }

        #region Zeitkonto

        private void UpdateTaskbar()
        {
            ulong max = (ulong)timeAccount.DefaultPresenceAmount.TotalMilliseconds;
            ulong curr = (ulong)timeAccount.CurrentPresence.TotalMilliseconds;

            if (max < curr)
            {
                max = (ulong)(max + timeAccount.DefaultMaxOvertime.TotalMilliseconds);
                taskBar.SetProgressState(this.handle, TBPFLAG.TBPF_INDETERMINATE);
            }
            else
            {
                taskBar.SetProgressState(this.handle, TBPFLAG.TBPF_NORMAL);
            }

            taskBar = (ITaskbarList3)new TaskbarList();
            taskBar.SetProgressValue(this.handle, curr, max);
        }

        void MainWindow_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.UpdateTaskbar();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (this.taskBar != null)
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(taskBar);
            }
        }

        #endregion Zeitkonto

        private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                System.Windows.Controls.TextBox box = sender as System.Windows.Controls.TextBox;

                string text = box.Text;
                box.Text = string.Empty;

                ScenarioVM currentScenarion = this.ScenarioTabControl.SelectedItem as ScenarioVM;
                if (currentScenarion != null)
                {
                    currentScenarion.AddTasks(text);
                }
            }
        }

        private void OnPriorityChange(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            if (fe != null)
            {
                TaskVM task = fe.DataContext as TaskVM;

                if (task != null)
                {
                    switch (task.TaskPriority)
                    {
                        case Priority.A:
                            task.TaskPriority = Priority.B;
                            break;
                        case Priority.B:
                            task.TaskPriority = Priority.C;
                            break;
                        case Priority.C:
                            task.TaskPriority = Priority.none;
                            break;
                        case Priority.special:
                            task.TaskPriority = Priority.A;
                            break;
                        default:
                            task.TaskPriority = Priority.special;
                            break;
                    }
                }
            }
        }

        private void OnIsRunningChanged(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            if (fe != null)
            {
                TaskVM task = fe.DataContext as TaskVM;

                if (task != null)
                {
                    if (task.IsRunning)
                    {
                        TimeMeasure.Stop(task);
                    }
                    else
                    {
                        TimeMeasure.Start(task);
                    }
                }
            }
        }

        private void OnWindowIsClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            RepositoryVM.Center.Save();
        }

        public void BindKeys()
        {
            // Found at: http://stackoverflow.com/questions/48935/c-using-wpf-and-net-3-5-how-can-i-register-a-global-hot-key-to-say-ctrlshift
            // Thanks to: http://stackoverflow.com/users/1695/gishu

            // Bind Key
            this.InputBindings.Add(new InputBinding(CommandCollection.Save, new KeyGesture(Key.S, ModifierKeys.Control)));
            this.InputBindings.Add(new InputBinding(new FocusMoveCommand(this.inputBar), new KeyGesture(Key.N, ModifierKeys.Control)));
            this.InputBindings.Add(new InputBinding(CommandCollection.ExpandTracer, new KeyGesture(Key.T, ModifierKeys.Control | ModifierKeys.Shift)));
            this.InputBindings.Add(new InputBinding(CommandCollection.ExpandTimeAccount, new KeyGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift)));

            // Bind handler       CommandBinding cb = new CommandBinding( MyAppCommands.SaveAll);       cb.Executed += new ExecutedRoutedEventHandler( HandlerThatSavesEverthing );       this.CommandBindings.Add (cb );
        }
    }
}